//
//  HTClassPayManager.swift
//  QuitSmokingApp
//
//  Created by z on 2024/3/18.
//

import UIKit
import MBProgressHUD

//hud延迟消失时间
let var_kHudHiddenTime = 1.2

class HTClassHudView: MBProgressHUD {
    
    static func ht_showLoading() -> HTClassHudView? {
        if let var_topVC = HTClassTool.ht_topVC() {
            let var_hudview = HTClassHudView().ht_showHudOnView(var_view: var_topVC.view)
            var_hudview.mode = MBProgressHUDMode.indeterminate;
            var_hudview.contentColor = .white
            var_hudview.completionBlock = {}
            return var_hudview
        }
        return nil
    }
    
    
    public func ht_showLoadingOnView(var_view: UIView, var_completedBlock:((HTClassHudView) -> ())?) -> (HTClassHudView){
        let var_hudview = HTClassHudView().ht_showHudOnView(var_view: var_view)
        var_hudview.mode = MBProgressHUDMode.indeterminate;
        var_hudview.contentColor = .white
        var_hudview.completionBlock = {
            var_completedBlock?(var_hudview)
        }
        return var_hudview
    }
    
    //展示文本toast
    public func ht_showToastOnView(var_view: UIView, var_title: String, var_completedBlock:((HTClassHudView) -> ())?) -> (HTClassHudView){
        let var_hudview = HTClassHudView().ht_showHudOnView(var_view: var_view)
        var_hudview.margin = 10
        var_hudview.mode = MBProgressHUDMode.text;
        var_hudview.label.text = var_title
        var_hudview.label.textColor = .white;
        var_hudview.label.font = .systemFont(ofSize: 13);
        var_hudview.label.numberOfLines = 0;
        var_hudview.completionBlock = {
            var_completedBlock?(var_hudview)
        }
        return var_hudview
    }

    //展示带图标的大文本toast
    public func ht_showLargeToastOnView(var_view: UIView, var_title: String, var_iconImage: String, var_completedBlock:((HTClassHudView) -> ())?) -> (HTClassHudView){
        let var_hudview = HTClassHudView().ht_showHudOnView(var_view: var_view)
        var_hudview.margin = 30
        var_hudview.mode = MBProgressHUDMode.customView;
        if !var_iconImage.isEmpty {
            var_hudview.customView = UIImageView(image: UIImage(named: var_iconImage))
        }
        var_hudview.label.text = var_title
        var_hudview.label.textColor = .white;
        var_hudview.label.font = .systemFont(ofSize: 24, weight: .bold);
        var_hudview.label.numberOfLines = 0;
        var_hudview.bezelView.color = UIColor("#212329CC").withAlphaComponent(0.8)
        var_hudview.bezelView.layer.cornerRadius = 16
        var_hudview.bezelView.layer.borderColor = UIColor("#298CB2").cgColor
        var_hudview.bezelView.layer.borderWidth = 1.0
        var_hudview.completionBlock = {
            var_completedBlock?(var_hudview)
        }
        return var_hudview
    }

    private func ht_showHudOnView(var_view: UIView) -> (HTClassHudView){
        var var_containerView : UIView? = var_view
        if var_containerView == nil {
            var_containerView = (UIApplication.shared.delegate?.window!!)! as UIView
        }
        
        let var_lastHud = HTClassHudView.forView(var_containerView!)
        var var_hud = HTClassHudView()
        if var_lastHud != nil {
            var_lastHud?.hide(animated: false)
            var_hud = HTClassHudView.showAdded(to: var_containerView!, animated: false)
        }else{
            var_hud = HTClassHudView.showAdded(to: var_containerView!, animated: true)
        }
        
        var_hud.bezelView.style = .solidColor
        var_hud.bezelView.color = UIColor(red: 42/255.0, green: 48/255.0, blue: 56/255.0, alpha: 1.0)
        var_hud.isUserInteractionEnabled = true
        return var_hud
    }

}
